<template>
  <u-popup closeable border-radius="28" @close="close" mode="bottom" height="80%" v-model="show">
    <view class="search-wrapper">
      <input v-model="searchKeyword" placeholder="搜索经纪人" />
      <button @click="filterBrokers" style="background-color: #ccc">搜索</button>
    </view>
    <view class="broker-list">
      <view class="broker-item" v-for="(broker, index) in filteredBrokerList" :key="index">
        <view class="broker-info-wrapper">
          <view class="broker-name">{{ broker.name }}</view>
          <view class="broker-phone">{{ broker.phone }}</view>
        </view>
        <button class="select-btn" :style="{ backgroundColor: selectedBroker && selectedBroker.id === broker.id ? '#007aff' : '#ccc' }" @click="selectBroker(broker)">选择</button>
      </view>
    </view>
    <view class="confirm-btn-wrapper">
      <button class="confirm-btn" @click="confirmSelect" :disabled="!selectedBroker">确认选择</button>
    </view>
  </u-popup>
</template>

<script>
export default {
  props: {
    show: {
      type: Boolean,
      default: false
    },
    brokerList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      searchKeyword: '',
      selectedBroker: null,
      filteredBrokerList: this.brokerList
    };
  },
  watch: {
    show(newVal) {
      if (!newVal) {
        this.reset();
      }
    },
    brokerList(newList) {
      this.filteredBrokerList = newList;
    }
  },
  methods: {
    close() {
      this.$emit('close');
    },
    selectBroker(broker) {
      this.selectedBroker = broker;
    },
    confirmSelect() {
      if (this.selectedBroker) {
        this.$emit('confirm', this.selectedBroker);
        this.reset();
      }
    },
    filterBrokers() {
      if (!this.searchKeyword) {
        this.filteredBrokerList = this.brokerList;
      } else {
        const keyword = this.searchKeyword.toLowerCase();
        this.filteredBrokerList = this.brokerList.filter(broker => {
          return broker.name.toLowerCase().includes(keyword) || broker.phone.includes(keyword);
        });
      }
    },
    reset() {
      this.searchKeyword = '';
      this.selectedBroker = null;
      this.filteredBrokerList = this.brokerList;
    }
  }
};
</script>

<style scoped>
.search-wrapper {
  padding: 20rpx;
  display: flex;
  align-items: center;
}
.search-wrapper input {
  flex: 1;
  padding: 10rpx;
  border: 1rpx solid #ccc;
  border-radius: 5rpx;
  margin-right: 10rpx;
}
.search-wrapper button {
  padding: 10rpx 20rpx;
  background-color: #007aff;
  color: #fff;
  border: none;
  border-radius: 5rpx;
  font-size: 28rpx;
}
.broker-list {
  padding: 20rpx;
  overflow-y: auto;
}
.broker-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10rpx;
  border-bottom: 1rpx solid #ccc;
}
.broker-info-wrapper {
  display: flex;
  flex: 1;
}
.broker-name {
  flex: 1;
}
.broker-phone {
  flex: 1;
  margin-left: 10rpx;
}
.select-btn {
  padding: 5rpx 20rpx;
  color: #fff;
  border: none;
  border-radius: 5rpx;
  font-size: 20rpx;
  min-width: 80rpx;
}
.confirm-btn-wrapper {
  padding: 20rpx;
  text-align: center;
}
.confirm-btn {
  padding: 10rpx 20rpx;
  background-color: #007aff;
  color: #fff;
  border: none;
  border-radius: 5rpx;
  font-size: 28rpx;
}
.confirm-btn:disabled {
  background-color: #ccc;
}
</style>